layui.use(['form','jquery','layer','table'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    getProductSalesReturnOrders();
    getProductCountsByStatus();
    //刷新按钮
    $('#refresh').click(function () {
        getProductSalesReturnOrders();
    });
    //高级检索按钮
    $('#advancedSearch').click(function () {
        layer.open({
            type: 2,
            title: '高级检索',
            content: '/orderForm/productOrder/productOrderList/product-order-advanced-search.html',
            area: ['900px', '510px'],
            offset: ['10px', '200px'],
            shade: 0.4,
            cancel: function(index, layero){
                layer.close(index);
                getProductSalesReturnOrders();
                return false;
            },
            end: function () {
                var ordersData = $("#ordersData").data("ordersData");
                if(ordersData == undefined){
                    getProductSalesReturnOrders();
                }else{
                    ordersRender(ordersData);
                    $('.buttonBox').children('.orderStatusButton').addClass('layui-btn-primary');
                }
            }
        })
    });
    //订单来源、订单支付渠道查询订单
    form.on('submit(search)',function (obj) {
        getProductSalesReturnOrders(obj.field);
        return false;
    });
    //返回按钮
    $('#goBack').click(function () {
        var layIdArr = localStorage.getItem("layIdArr");
        layIdArr = layIdArr.split(",");
        layIdArr.pop();
        window.parent.layui.element.tabChange('tab',layIdArr[layIdArr.length - 1]);
        localStorage.setItem("layIdArr",layIdArr);
    });
    //退货申请按钮点击
    $('#returnApplication').click(function () {
        getProductSalesReturnOrdersByStatus(7);
    });
    //退货中按钮点击
    $('#inReturn').click(function () {
        getProductSalesReturnOrdersByStatus(8);
    });
    //已退货按钮点击
    $('#returned').click(function () {
        getProductSalesReturnOrdersByStatus(9);
    });
    //拒绝退货按钮点击
    $('#rejectReturn').click(function () {
        getProductSalesReturnOrdersByStatus(11);
    });
    //状态按钮切换效果
    $('.buttonBox').children('.orderStatusButton').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
    });

    //收起按钮
    $('#packUp').click(function () {
        if($(this).text() == ' 收起'){
            $('.layui-card-body').slideUp(300,'linear');
            $('#packUp').text(' 展示').removeClass('layui-icon-down').addClass('layui-icon-up');
        }else if($(this).text() == ' 展示'){
            $('.layui-card-body').slideDown(300,'linear');
            $('#packUp').text(' 收起').removeClass('layui-icon-up').addClass('layui-icon-down');
        }
    })
    //根据不同的状态按钮查询不同的数据
    function getProductSalesReturnOrdersByStatus(status) {
        $.ajax({
            url: '/SyUserOrder/getProductDisposeOrdersByStatusPaySource',
            data: {'status': status},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                ordersRender(data.data.data);
            }
        });
    }
    function getProductCountsByStatus() {
        //退货申请的订单数量
        $.ajax({
            url: '/SyUserOrder/getProductCountsByStatus',
            data: {'status':7},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                $('#returnApplicationCounts').text(data.data);
            }
        });
        //退货中的订单数量
        $.ajax({
            url: '/SyUserOrder/getProductCountsByStatus',
            data: {'status':8},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                $('#inReturnCounts').text(data.data);
            }
        });
        //已退货的订单数量
        $.ajax({
            url: '/SyUserOrder/getProductCountsByStatus',
            data: {'status':9},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                $('#returnedCounts').text(data.data);
            }
        });
        //拒绝退货的订单数量
        $.ajax({
            url: '/SyUserOrder/getProductCountsByStatus',
            data: {'status':11},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                $('#rejectReturnCounts').text(data.data);
            }
        });
    }
    function getProductSalesReturnOrders(StatusSourcePay) {
        $.ajax({
            url: '/SyUserOrder/getProductSalesReturnOrders',
            data: StatusSourcePay,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                ordersRender(data.data.data);
                $('.buttonBox').children('.orderStatusButton').addClass('layui-btn-primary');
            }
        });
    }
    //将查出来的数据渲染到表格中
    function ordersRender(data) {
        table.render({
            elem: '#ordersTable',
            title: '审核列表',
            data: data,
            limit: 9,
            width: 1630,
            size: 'lg',
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',fixed:'left'},
                {title:'序号',align:'center',width:170,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"orderLogisticsNumber",title:"订单号",align:'center',width:170},
                {field:"beforeAccount",title:"用户昵称",align:'center',width:170},
                {field:"beforePhone",title:"用户手机",align:'center',width:170},
                {field:"orderStatus",title:"订单状态",align:'center',width:170,templet:function(data){
                        if(data.orderStatus == 7){
                            return '<p>退货申请</p>';
                        }else if(data.orderStatus == 8){
                            return '<p>退货中</p>';
                        }else if(data.orderStatus == 9){
                            return '<p>已退货</p>';
                        }else if(data.orderStatus == 11){
                            return '<p>拒绝退货</p>';
                        }
                    }},
                {field:"productName",title:"商品名字",align:'center',width:180},
                {field:"regionName",title:"发货信息",align:'center',width:180},
                {field:"userAccount",title:"发货人",align:'center',width:180},
                {field:'',title:"操作",align:'center',width:180,toolbar: '#operation'}
            ]]
        });
    }
    table.on('tool(ordersTable)',function (data) {
        //查看订单表详情信息
        if (data.event == 'selectTable') {
            layer.open({
                type: 2,
                title: '订单详情',
                content: '/orderForm/productOrder/productOrderList/product-detailed-information.html',
                area: ['1150px', '550px'],
                offset: ['10px', '50px'],
                shade: 0.4,
                btn:['确定'],
                yes:function(index,layero){
                    layer.close(index);
                },
                success:function(layero,index) {
                    getProductByOrderId(data.data.orderId,layero);
                    getProductBySalesVolume(data.data.shopId,layero)
                }

            })
            //删除按钮
        }else if(data.event == 'deleteTable'){
            delCurrentOrder(data.data.orderId);
        }
    })
    //删除当前行订单
    function delCurrentOrder(orderId) {
        $.ajax({
            url: '/SyUserOrder/deleteCurrentOrder',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                layer.msg(data.data);
                getProductSalesReturnOrders();
                getProductCountsByStatus();
            }
        });
    }
    //获取商品销量
    function getProductBySalesVolume(shopId,layero) {
        $.ajax({
            url: '/product/getProductBySalesVolume',
            dataType: 'json',
            type: 'post',
            data: {shopId: shopId},
            success: function (data) {
                var product = data.data;
                var childHtml = layero.find('iframe').contents();
                childHtml.find('#productSales').text(product.productSales);
            }
        })
    }
    //查看当前订单有关信息
    function getProductByOrderId(orderId,layero) {
        $.ajax({
            url: '/product/getProductByOrderId',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                var product = data.data;
                var childHtml = layero.find('iframe').contents();
                childHtml.find('#productCode').text(product.productCode);
                childHtml.find('#productName').text(product.productName);
                childHtml.find('#currentpriceProduct').text(product.currentpriceProduct);
                childHtml.find('#categoryTitle').text(product.categoryTitle);
                var productStatus;
                if(product.productSate == 0){
                    productStatus = '已删除'
                }else if(product.productSate == 1){
                    productStatus = '上架'
                }else if(product.productSate == 2){
                    productStatus = '下架'
                }
                childHtml.find('#productSate').text(productStatus);
                childHtml.find('#productLabel').text(product.productLabel);
                childHtml.find('#hits').text(product.hits);
                childHtml.find('#collectCount').text(product.productCollectionCount);
                childHtml.find('#productDescription').text(product.productDescription);
                childHtml.find('#mainImage').attr('src',product.mainImage);

            }
        });
    }
})